package net.sashag.nadlandroid;

import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;

import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.api.Scope;
import com.parse.ParseException;
import com.parse.ParseUser;

@EBean(scope = Scope.Singleton)
public class FavoritesManager {
	
	@Bean protected ProximityAlertManager proximityAlertManager;
	
	public void setProximityAlertsFromFavoriteApartments(ParseUser user) {
		if (!proximityAlertManager.useProximityAlerts())
			return;
		
		retrieveFavoriteApartmentsAndSetAlerts(user);
	}
	
	public void removeAllProximityAlerts(ParseUser user) {
		String[] favorites = getFavoriteApartmentIds(user);
		for (String apartmentId : favorites) {
			proximityAlertManager.removeProximityAlert(apartmentId);
		}
	}
	
	@Background
	protected void retrieveFavoriteApartmentsAndSetAlerts(ParseUser user) {
		String[] favorites = getFavoriteApartmentIds(user);
		for (int i = 0; i < favorites.length; ++i) {
			try {
				Apartment apartment = Apartment.fromId(favorites[i]);
				proximityAlertManager.addProximityAlert(favorites[i], apartment.getLatitude(), apartment.getLongitude());
			} catch (ParseException e) {
				Log.w("ProximityAlertManager", "Error retrieving favorite apartment details: " + e.getMessage());
			}
		}
	}
	
	public void addToFavorites(Apartment apartment, ParseUser user) {
		JSONArray favorites = user.getJSONArray("Favorites");
		if (favorites == null) {
			favorites = new JSONArray();
		}
		String apartmentId = apartment.getObjectId();
		for (int i = 0; i < favorites.length(); ++i) {
			try {
				if (favorites.getString(i).equals(apartmentId))
					return; //This favorite already exists
			} catch (JSONException e) {
				Log.w("FavoritesManager", "Error parsing favorites array: " + e.getMessage());
			} 
		}
		favorites.put(apartmentId);
		user.put("Favorites", favorites);
		user.saveInBackground();
		
		proximityAlertManager.addProximityAlert(apartmentId, apartment.getLatitude(), apartment.getLongitude());
	}
	
	public String[] getFavoriteApartmentIds(ParseUser user) {
		JSONArray favoritesArr = user.getJSONArray("Favorites");
		if (favoritesArr == null || favoritesArr.length() == 0) {
			return new String[0];
		}
		String[] favorites = new String[favoritesArr.length()];
		for (int i = 0; i < favoritesArr.length(); ++i) {
			try {
				favorites[i] = favoritesArr.getString(i);
			} catch (JSONException e) {
				Log.w("Favorites", "Error parsing favorites JSON: " + e.getMessage());
			}
		}
		return favorites;
	}

	public void removeFromFavorites(String apartmentId, ParseUser user) {
		JSONArray favoritesArr = user.getJSONArray("Favorites");
		JSONArray newFavorites = new JSONArray();
		for (int i = 0; i < favoritesArr.length(); ++i) {
			String favorite;
			try {
				favorite = favoritesArr.getString(i);
				if (!favorite.equals(apartmentId)) {
					newFavorites.put(favorite);
				}
			} catch (JSONException e) {
				Log.w("Favorites", "Error parsing favorites JSON: " + e.getMessage());
			}
		}
		user.put("Favorites", newFavorites);
		user.saveInBackground();
	}
	
}
